/****************************************************************************
 *
 *   Copyright (c) 2019-2021 PX4 Development Team. All rights reserved.
 *   Copyright (c) 2025 H743 Flight Controller Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#pragma once

#include "work_queue_manager.hpp"
#include <px4_platform_common/intrusive_queue.h>
#include <px4_platform_common/containers/intrusive_sorted_list.h>
#include <px4_platform_common/px4_sem.h>
#include "FreeRTOS.h"
#include "task.h"

namespace px4
{

// Forward declaration
class WorkItem;

/**
 * WorkQueue - Individual work queue instance
 *
 * Manages a queue of work items that are executed sequentially in a dedicated thread.
 * Work items can be:
 * - Attached: Added to the work queue's list of managed items
 * - Detached: Removed from the work queue's list
 * - Added: Scheduled to run (added to runnable queue)
 * - Removed: Unscheduled (removed from runnable queue)
 */
class WorkQueue
{
public:
	explicit WorkQueue(const wq_config_t &config);
	~WorkQueue();

	// Non-copyable
	WorkQueue(const WorkQueue &) = delete;
	WorkQueue &operator=(const WorkQueue &) = delete;

	const char *get_name() const { return _config.name; }
	const wq_config_t &get_config() const { return _config; }

	/**
	 * Attach a work item to this work queue
	 * @param item Work item to attach
	 * @return true if successful
	 */
	bool Attach(WorkItem *item);

	/**
	 * Detach a work item from this work queue
	 * @param item Work item to detach
	 */
	void Detach(WorkItem *item);

	/**
	 * Add a work item to the runnable queue (schedule it to run)
	 * @param item Work item to add
	 */
	void Add(WorkItem *item);

	/**
	 * Remove a work item from the runnable queue (unschedule it)
	 * @param item Work item to remove
	 */
	void Remove(WorkItem *item);

	/**
	 * Clear all work items from the runnable queue
	 */
	void Clear();

	/**
	 * Main run loop (called by WorkQueueManager task)
	 */
	void Run();

	/**
	 * Request work queue to stop
	 */
	void request_stop();

	/**
	 * Print work queue status
	 */
	void print_status(bool last);

	// Intrusive list node for WorkQueueManager's list
	intrusive_sorted_list_node_t sorted_list_node;

	// Comparison operator for sorted list (sorted by relative priority)
	static bool compare_by_priority(const void *a, const void *b)
	{
		const WorkQueue *wq_a = (const WorkQueue *)a;
		const WorkQueue *wq_b = (const WorkQueue *)b;
		return wq_a->_config.relative_priority >= wq_b->_config.relative_priority;
	}

private:
	/**
	 * Signal the worker thread that work is available
	 */
	void SignalWorkerThread();

	const wq_config_t &_config;

	// Runnable queue (FIFO queue of work items ready to run)
	intrusive_queue_t _q;

	// Attached work items (sorted list of all work items managed by this queue)
	intrusive_sorted_list_t _work_items;

	// Synchronization
	px4_sem_t _process_lock;  // Semaphore to wait for work
	px4_sem_t _qlock;         // Mutex for queue operations

	bool _should_exit{false};
};

} // namespace px4

